<template>
  <div class="home">
    <div class="detail_box">
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>关键词：</label>
            <el-input type="text" placeholder="请输入关键词" v-model="keyword"></el-input>
          </div>
          <div class="operate_item">
            <label>类型：</label>
            <el-select
              v-model="typeId"
              placeholder="请选择类型">
              <el-option
                v-for="(item,index) in typeOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&btn.add">
          <span class="operate_btn search_btn" @click="openPopup('',1,0)" >新增</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table_box">
        <el-table
          border
          v-loading="loading"
          :header-cell-style="tableHeader"
          ref="multipleTable"
          :data="tableInfo"
          tooltip-effect="dark"
          style="width: 100%;">
          <el-table-column
            width="80"
            prop="id"
            label="ID">
          </el-table-column>
          <el-table-column
            sortable
            prop="modifyTime"
            label="修改时间">
            <template slot-scope="scope">
              <p>{{formatTime(scope.row.modifyTime)}}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="渠道名称">
          </el-table-column>
          <el-table-column
            label="消息类型">
            <template slot-scope="scope">
              <p>{{scope.row.noticeName?scope.row.noticeName:'——'}}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="config"
            label="配置">
          </el-table-column>
          <el-table-column
            width="80"
            label="状态">
            <template slot-scope="scope">
              <p class="normal" :class="{'unusual':scope.row.enable===0}">{{scope.row.enable===1?'可用':'禁用'}}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            v-if="btn&&(btn.delete||btn.update)"
            fixed="right"
            width="150">
            <template slot-scope="scope">
              <div class="table_btn">
                <span class="change_info" @click="openPopup(scope.row.id)" v-if="btn.update">编辑</span>
                <span class="change_info delete_btn" @click="deleteItem(scope.row.id)" v-if="btn.delete">删除</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{totals}}</i>条</span>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getTableInfo"
          background
          :current-page="currentPage"
          :page-sizes="[15, 20, 30, 40]"
          :page-size="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="totals">
        </el-pagination>
      </div>
    </div>

    <edit v-if="isPop" :channelId="channelId" @listenToChild="listenToEdit"></edit>
  </div>
</template>

<script>
  import edit from './edit'
  import commons from "@/utils//common";
  import {getBtn} from "@/utils/btnAuth";
  import {deleteMessageChannel, getMessageChannel, getMessageChannelType} from "../../../api/data/channel";

  export default {
    components: {
      edit
    },
    data() {
      return {
        typeId:'',
        typeOption:[],
        keyword:'',
        currentPage: 1,
        pageSize: 15,
        totals: 0,
        loading: false,
        tableInfo:[],
        isDetail:false,
        channelId:'',
        isPop:false,
        btn:{},
      }
    },
    mounted() {
      getBtn().then(res=>{
        this.btn = res;
        if (this.btn.query) {
          this.getTableInfo(1);
        }
      })
      this.getType();
    },
    methods: {
      // 获取列表信息
      getTableInfo(page) {
        let that = this;
        that.loading = true;
        that.currentPage = page;
        let queryParams = {
          limit:that.pageSize,
          page:page,
          noticeType:that.typeId,
          keyword:that.keyword
        }
        getMessageChannel(queryParams).then(res => {
          that.tableInfo = res.data?res.data.list:[];
          that.totals = res.data?res.data.count:0;
          that.loading = false;
        }, (err) => {
          that.loading = false;
        });
      },

      // 重置
      resetTable() {
        this.pageSize = 15;
        this.keyword = '';
        this.typeId = '';
        this.getTableInfo(1)
      },

      // 获取消息类型
      getType(){
        getMessageChannelType().then(res => {
          this.typeOption = res.data ? res.data :[];
        });
      },

      openPopup(id){
        this.isPop = true;
        this.channelId = id;
      },

      listenToEdit(e){
        this.isPop = false;
        if(e.refresh){
          this.getTableInfo(this.currentPage);
        }
      },

      deleteItem(id){
        let that = this;
        that.$confirm('确定删除渠道信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          that.loading = true;
          deleteMessageChannel(id).then(res => {
            if (res.success) {
              that.getTableInfo(that.currentPage);
              that.$message.success('删除成功！');
            }
            that.loading = false;
          }, (err) => {
            that.loading = false;
            that.$message.error('删除失败，请重试');
          });
        }).catch(() => {
          // console.log('fail')
        });
      },

      formatTime(time){
        return commons.formatTime(time)
      },

      sizeChange(val){
        this.pageSize =val;
        this.getTableInfo(1)
      },

      // 设置table标题居中
      tableHeader({row, column, rowIndex, columnIndex}) {
        return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
      },
    }
  }
</script>

<style scoped>

</style>
